package cn.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.bean.Play;
import cn.bean.Ticket;
import cn.bean.User;
import cn.dao.PlayDao;
import cn.dao.TicketDao;
import cn.dao.UserDao;
import cn.dbc.BaseDao;

public class TicketDaoImpl implements TicketDao {

	BaseDao bs = new BaseDao();
	PlayDaoImpl pd = new PlayDaoImpl();
	UserDaoImpl ud = new UserDaoImpl();
	@Override
	public Ticket findTicketById(int id) {
		Ticket t = new Ticket();
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null){
			String sql = "select * from ticket where id="+id;
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				
				if(rs.next()){
					t.setId(rs.getInt(1));
					t.setPlay(pd.findPlayById(rs.getInt(2)));
					t.setUser(ud.findUserById(rs.getInt(3)));
					t.setSeat(rs.getInt(4));
					t.setBuy_time(rs.getString(5));
					
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			finally{
				bs.closeConnection(conn, pstmt, rs);
			}
		}
		
		
		return t;
	}

	@Override
	public List<Ticket> findTicketByUser(User user) {
		List<Ticket> list = new ArrayList<Ticket>();
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null){
			String sql = "select * from ticket where fk_user_id="+user.getId();
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				
				while(rs.next()){
					Ticket t = new Ticket();
					t.setId(rs.getInt(1));
					t.setPlay(pd.findPlayById(rs.getInt(2)));
					t.setUser(ud.findUserById(rs.getInt(3)));
					t.setSeat(rs.getInt(4));
					t.setBuy_time(rs.getString(5));
					list.add(t);
					
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			finally{
				bs.closeConnection(conn, pstmt, rs);
			}
		}
		
		
		return list;
	}

	@Override
	public List<Ticket> findTicketByPlay(Play play) {
		List<Ticket> list = new ArrayList<Ticket>();
		Connection conn = bs.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		if(conn!=null){
			String sql = "select * from ticket where fk_play_id="+play.getId();
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				
				while(rs.next()){
					Ticket t = new Ticket();
					t.setId(rs.getInt(1));
					t.setPlay(pd.findPlayById(rs.getInt(2)));
					t.setUser(ud.findUserById(rs.getInt(3)));
					t.setSeat(rs.getInt(4));
					t.setBuy_time(rs.getString(5));
					list.add(t);
					
				}
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
			finally{
				bs.closeConnection(conn, pstmt, rs);
			}
		}
		
		
		return list;
	}

	@Override
	public boolean doTicket(Play play, User user, int seat, String buy_time) {
		String sql = "insert into ticket(fk_play_id,fk_user_id,seat,buy_time) values(?,?,?,?)";
		List<Object> ls = new ArrayList<Object>();
		ls.add(play.getId());
		ls.add(user.getId());
		ls.add(seat);
		ls.add(buy_time);
		boolean isFlag = bs.update(sql, ls);
		return isFlag;
	
	}

	@Override
	public boolean deleteTicket(int id) {
		String sql = "delete from ticket where id="+id;
		List<Object> ls = new ArrayList<Object>();
		
		boolean isFlag = bs.update(sql, ls);
		return isFlag;
	
	}

	@Override
	public boolean deleteTicketByPlayId(int id) {
		String sql = "delete from ticket where fk_play_id="+id;
		List<Object> ls = new ArrayList<Object>();
		
		boolean isFlag = bs.update(sql, ls);
		return isFlag;
	}
	

	

}
